package com.jeady.timetodo.database

import androidx.room.ColumnInfo
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.Update

@Dao
interface TaskDao {
    @Query("SELECT * FROM task")
    fun getAll(): List<Task>

    @Query("SELECT * FROM task WHERE id IN (:taskIds)")
    fun getTasksByTaskIds(taskIds: IntArray): List<Task>

    @Query("SELECT * FROM task WHERE tid=:taskId")
    fun getTasksByTaskId(taskId: Long): List<Task>

    @Query("SELECT * FROM task WHERE state in (:runningFlag)")
    fun findRunningTasks(runningFlag: ArrayList<String>): Task

    @Query("DELETE FROM task")
    fun deleteAll()

    @Update
    fun updateTaskState(task: Task)

    @Insert
    fun insert(tasks: List<Task>)

    @Insert
    fun insert(vararg task: Task)

    @Delete
    fun delete(task: Task)
}

@Entity
data class Task(
    @PrimaryKey(autoGenerate = true) val id: Int=0,
    @ColumnInfo(name = "tid") val taskId: Long =0,
    @ColumnInfo(name = "type") val type: String="",
    @ColumnInfo(name = "title") val title: String="",
    @ColumnInfo(name = "description") val description: String="",
    @ColumnInfo(name = "state") val state: String="",
    @ColumnInfo(name = "start_time") val startTime: Long=0,
    @ColumnInfo(name = "interval") val interval: Long=0,
    @ColumnInfo(name = "end_time") val endTime: Long=0,
    @ColumnInfo(name = "create_at", defaultValue = "CURRENT_TIMESTAMP")
    val createAt: String="",
    @ColumnInfo(name = "update_at", defaultValue = "CURRENT_TIMESTAMP")
    val updateAt: String="",
)
